import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.log4j.Logger;

public class CreateMultipleProcess implements JavaDelegate {
	private static Logger logger = Logger.getLogger("CreateMultipleProcess");

	@Override
	public void execute(DelegateExecution arg0) throws Exception {
		int count = (Integer) arg0.getVariable("IEcount");
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		final String id = arg0.getProcessInstanceId();
		final RuntimeService runtimeService = processEngine.getRuntimeService();
		String[] subProcesses = new String[count];
		for (int i = 0; i < count; i++) {
			Map<String, Object> variables = new HashMap<String, Object>();
			variables.put("processId", id);
			ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("IE", variables);
			subProcesses[i] = processInstance.getProcessInstanceId();
			logger.info(processInstance.getProcessInstanceId());
		}
		arg0.setVariable("ids", subProcesses);
	}

}
